package com.mininglamp.main;

import com.mininglamp.mode.Input;
import org.drools.core.event.DefaultAgendaEventListener;
import org.kie.api.KieServices;
import org.kie.api.definition.rule.Rule;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.event.rule.BeforeMatchFiredEvent;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.api.runtime.conf.TimedRuleExecutionFilter;
import org.kie.api.runtime.conf.TimedRuleExecutionOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouhuiyu on 17/9/10.
 */
public class TestSessionConfigMain {

    protected static final Logger logger          = LoggerFactory.getLogger( TestSessionConfigMain.class);
    public static void main(String[] args) {

        KieServices ks = KieServices.Factory.get();
        //KieContainer kContainer = ks.newKieContainer(ks.newReleaseId("org.slf4j", "slf4j-api", "1.5.6"));

        KieContainer kContainer = ks.getKieClasspathContainer();


//      2.18.2. Automatically firing timed rule in passive mode
//      When the rule engine runs in passive mode (i.e.: using fireAllRules) by default it doesn’t fire consequences of timed rules unless fireAllRules isn’t invoked again.
//      Now it is possible to change this default behavior by configuring the KieSession with a TimedRuleExectionOption as shown in the following example.
//        Example 17. Configuring a KieSession to automatically execute timed rules
//        KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
//        ksconf.setOption( TimedRuleExectionOption.YES );
//        KSession ksession = kbase.newKieSession(ksconf, null);
//        It is also possible to have a finer grained control on the timed rules that have to be automatically executed.
//        To do this it is necessary to set a `FILTERED`TimedRuleExectionOption that allows to define a callback to filter those rules, as done in the next example.
//
//                Example 18. Configuring a filter to choose which timed rules should be automatically executed
//        KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
//        conf.setOption( new TimedRuleExectionOption.FILTERED(new TimedRuleExecutionFilter() {
//            public boolean accept(Rule[] rules) {
//                return rules[0].getName().equals("MyRule");
//            }
//        }) );
        KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
        //ksconf.setOption(TimedRuleExecutionOption.YES);
        ksconf.setOption(new TimedRuleExecutionOption.FILTERED(new TimedRuleExecutionFilter() {
            public boolean accept(Rule[] rules) {
                logger.info("rulename=" + rules[0].getName());
                return rules[0].getName().equals("rule.4");
            }
        }
        ));


        StatelessKieSession kSession = kContainer.newStatelessKieSession("test002", ksconf);
        kSession.addEventListener(new DefaultAgendaEventListener() {
            public void afterMatchFired(AfterMatchFiredEvent event) {
                super.afterMatchFired( event );
                //System.out.println( event + " ========zhy after ====");
            }

            @Override
            public void beforeMatchFired(BeforeMatchFiredEvent event) {
                super.beforeMatchFired(event);
                //System.out.println();
                //System.out.println( event + " ========zhy before ====");
            }
        });

        Input input = new Input("刘磊", "男", "18", "湖南", 1.69D, 1500);

        kSession.setGlobal("input", input);

        kSession.execute(input);

        String name = ((Input)kSession.getGlobals().get("input")).getName();
        logger.info(name);

        List<Input> inputs = new ArrayList<Input>();
        for (int i =0; i < 1; i++){
            Input input1 = new Input("刘磊", "男", "18", "湖南", 1.69D, 1500);
            inputs.add(input1);
        }

        kSession.execute(inputs);
        
    }

}
